package com.orange.core.service.repairOrder.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.core.pojo.req.repairOrder.RepairOrderPageQuery;
import com.orange.core.service.repairOrder.RepairOrderService;
import com.orange.core.mapper.repairOrder.RepairOrderMapper;
import com.orange.core.mapper.repairProblemCategory.RepairProblemCategoryMapper;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.entity.repairOrder.RepairOrder;
import com.orange.core.pojo.entity.repairOrder.dto.RepairOrderDTO;
import com.orange.core.pojo.entity.repairOrder.vo.RepairOrderVO;
import com.orange.core.pojo.entity.repairProblemCategory.RepairProblemCategory;
import com.orange.core.pojo.req.repairOrder.RepairOrderPageReq;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author lwm
* @description 针对表【repair_order(报修管理表)】的数据库操作Service实现
* @createDate 2025-05-28 22:34:32
*/
@Service
@RequiredArgsConstructor
public class RepairOrderServiceImpl extends ServiceImpl<RepairOrderMapper, RepairOrder>
    implements RepairOrderService {

    private final RepairProblemCategoryMapper repairProblemCategoryMapper;

    @Override
    public Page<RepairOrderVO> page(PageReqDto<RepairOrderPageReq> pageReqDto) {
        if(pageReqDto.getCondition() == null){
            pageReqDto.setCondition(new RepairOrderPageReq());
        }
        RepairOrderPageReq condition = pageReqDto.getCondition();
        Page<RepairOrder> page = this.lambdaQuery()
                .eq(condition.getCategoryId() != null, RepairOrder::getCategoryId, condition.getCategoryId())
                .like(StrUtil.isNotBlank(condition.getTitle()), RepairOrder::getTitle, condition.getTitle())
                .eq(condition.getStatus() != null, RepairOrder::getStatus, condition.getStatus())
                .like(StrUtil.isNotBlank(condition.getHandlerName()),RepairOrder::getHandlerName,condition.getHandlerName())
                .ge(condition.getStartHandleTime() != null, RepairOrder::getHandleTime, condition.getStartHandleTime())
                .le(condition.getEndHandleTime() != null, RepairOrder::getHandleTime,
                        condition.getEndHandleTime() != null ? condition.getEndHandleTime().plusDays(1) : null)
                .page(pageReqDto.toPage());
        Page<RepairOrderVO> repairOrderVOPage = new Page<>(pageReqDto.getPage(), pageReqDto.getSize(),page.getTotal());
        //获取分类id列表
        List<Integer> categoryIds = page.getRecords()
                .stream().distinct()
                .map(RepairOrder::getCategoryId).collect(Collectors.toList());
        //获取分类map
        Map<Integer, RepairProblemCategory> repairProblemCategoryMap = repairProblemCategoryMapper.selectBatchIds(categoryIds)
                .stream()
                .collect(Collectors.toMap(RepairProblemCategory::getId, Function.identity()));


        List<RepairOrderVO> repairOrderVOList = page.getRecords()
                .stream().map(repairOrder -> {
            RepairOrderVO repairOrderVO = new RepairOrderVO();
            repairOrderVO.setCategoryName(Optional
                    .ofNullable(repairProblemCategoryMap
                            .get(repairOrder.getCategoryId()))
                    .map(RepairProblemCategory::getCategoryName).orElse(null));
            BeanUtil.copyProperties(repairOrder, repairOrderVO);
            return repairOrderVO;
        }).collect(Collectors.toList());
        repairOrderVOPage.setRecords(repairOrderVOList);
        return repairOrderVOPage;
    }

    @Override
    public RepairOrderVO info(Integer id) {
        RepairOrder repairOrder = this.getById(id);
        Assert.notNull(repairOrder, "报修单不存在");
        Integer categoryId = repairOrder.getCategoryId();
        RepairProblemCategory repairProblemCategory = repairProblemCategoryMapper.selectById(categoryId);
        RepairOrderVO repairOrderVO = new RepairOrderVO();
        BeanUtil.copyProperties(repairOrder, repairOrderVO);
        repairOrderVO.setCategoryName(repairProblemCategory.getCategoryName());
        return repairOrderVO;
    }

    @Override
    @Transactional
    public boolean save(RepairOrderDTO repairOrderDTO) {
        RepairOrder repairOrder = BeanUtil.copyProperties(repairOrderDTO, RepairOrder.class);
        boolean result = this.save(repairOrder);
        return result;
    }

    @Override
    @Transactional
    public boolean update(RepairOrderDTO repairOrderDTO) {
        RepairOrder repairOrder = BeanUtil.copyProperties(repairOrderDTO, RepairOrder.class);
        boolean result = this.updateById(repairOrder);
        return result;
    }

    @Override
    @Transactional
    public boolean deleteRepairOrderByIds(Integer[] ids) {
        boolean result = this.removeByIds(Arrays.asList(ids));
        return result;
    }

    @Override
    public Page<RepairOrderVO> pageByMerchantId(PageReqDto<RepairOrderPageQuery> pageReqDto) {
        Integer MerchantId = Optional.ofNullable(pageReqDto.getCondition()).map(RepairOrderPageQuery::getMerchantId).orElse(null);
        Page<RepairOrder> page = this.lambdaQuery()
                .eq(MerchantId != null, RepairOrder::getMerchantId, MerchantId)
                .orderByAsc(RepairOrder::getCreateTime)
                .page(pageReqDto.toPage());
        Page<RepairOrderVO> repairOrderVOPage = new Page<>(pageReqDto.getPage(), pageReqDto.getSize(),page.getTotal());

        //获取分类id列表
        List<Integer> categoryIds = page.getRecords()
                .stream().distinct()
                .map(RepairOrder::getCategoryId).collect(Collectors.toList());
        //获取分类map
        Map<Integer, RepairProblemCategory> repairProblemCategoryMap = repairProblemCategoryMapper.selectBatchIds(categoryIds)
                .stream()
                .collect(Collectors.toMap(RepairProblemCategory::getId, Function.identity()));

        List<RepairOrderVO> repairOrderVOList = page.getRecords()
                .stream().map(repairOrder -> {
                    RepairOrderVO repairOrderVO = new RepairOrderVO();
                    repairOrderVO.setCategoryName(Optional
                            .ofNullable(repairProblemCategoryMap
                                    .get(repairOrder.getCategoryId()))
                            .map(RepairProblemCategory::getCategoryName).orElse(null));
                    BeanUtil.copyProperties(repairOrder, repairOrderVO);
                    return repairOrderVO;
                }).collect(Collectors.toList());
        repairOrderVOPage.setRecords(repairOrderVOList);
        return repairOrderVOPage;
    }
}




